package com.young.common.util;

import java.io.Serializable;

/**
 * web restfull响应结果对象
 */
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int code = 0;//返回码
	private String info = "成功";//消息
	private T data;//返回结果
	private String callback = "";//jsonp回调使用
	private String unique;//唯一编码


	/**
	 * 结果码接口
	 * 实现此接口后可以通过new Result(errorCode)方式构造result对象
	 */
	public interface IResultCode{
		int code();
		String info();
		String detail();
	}

	/**
	 * 返回码枚举对象
	 */
	public enum CodeEnum implements IResultCode{
		SUCCESS(0, "成功", "成功"),
		FAIL(-1, "失败", "失败"),
		PARAM_INVALID(-11, "参数无效", "参数无效"),
		PARAM_ERROR(-12, "参数错误", "参数错误"),
		REPEAT_COMMIT(-13, "重复提交", "重复提交"),
		SESSION_TIMEOUT(-101, "会话超时", "会话超时"),
		BUSINESS_EXCEPTION(-102, "业务异常", "业务异常"),
		NOT_AUTH(-103, "无权限访问", "无权限访问"),
		SYSTEM_EXCEPTION(-999, "系统异常", "系统异常"),
		;
		int code;//结果码
		String info;//提示信息
		String detail;//详情信息

		CodeEnum(int code, String info, String detail) {
			this.code = code;
			this.info = info;
			this.detail = detail;
		}

		@Override
		public int code() {
			return code;
		}

		@Override
		public String info() {
			return info;
		}

		@Override
		public String detail() {
			return detail;
		}
	}

	/**
	 * 构造返回对象
	 * @param resultCode 结果码对象
	 * @return
	 */
	public static <T> Result<T> build(IResultCode resultCode){
		return build(resultCode.code(), resultCode.info());
	}

	/**
	 * 构造返回对象
	 * @param code
	 * @param info
	 * @param <T>
	 * @return
	 */
	public static <T> Result<T> build(int code, String info){
		return new Result<T>(code, info, null);
	}

	/**
	 * 构造成功返回对象
	 * @return 响应结果对象
	 */
	public static <T> Result<T> buildSuccess(){
		return build(CodeEnum.SUCCESS);
	}

	/**
	 * 构造成功返回对象
	 * @param info 返回消息
	 * @return 响应结果对象
	 */
	public static <T> Result<T> buildSuccessInfo(String info){
		return build(CodeEnum.SUCCESS.code(), info);
	}

	/**
	 * 构造成功返回对象
	 * @param obj 返回结果
	 * @param <T> 泛型
	 * @return 响应结果对象
	 */
	public static <T> Result<T> buildSuccess(T obj){
		if (obj == null) {
			return buildSuccess();
		}/* else if (obj instanceof List) {
			return new Result<T>(CodeEnum.SUCCESS.code(), CodeEnum.SUCCESS.info(), (T) obj);
		}*/
		else {
//			List<T> list = new ArrayList<T>();
//			list.add((T) obj);
			return new Result<T>(CodeEnum.SUCCESS.code(), CodeEnum.SUCCESS.info(), obj);
		}
	}

	/**
	 * 构造失败返回对象
	 * @return 响应结果对象
	 */
	public static <T> Result<T> buildFail(){
		return build(CodeEnum.FAIL);
	}

	/**
	 * 构造失败对象
	 * @param info 返回消息
	 * @return 响应结果对象
	 */
	public static Result buildFail(String info){
		return new Result<Page>(CodeEnum.FAIL.code(), info, null);
	}

	/**
	 * 无参构造器
	 */
	public Result(){
	}

	/**
	 * 带参构造器
	 * @param code 响应码
	 * @param info 响应消息
	 * @param data 返回数据
	 */
	public Result(int code, String info, T data){
		this.code = code;
		this.info = info;
		this.data = data;
	}

	/**
	 * 带参构造器
	 * @param code 响应码
	 * @param info 响应消息
	 */
	@Deprecated
	public Result(int code, String info){
		this.code = code;
		this.info = info;
	}

	/**
	 * 带参构造器
	 * @param list
	 */
	@Deprecated
	public Result(T list){
		this.data = list;
	}

//	/**
//	 * 带参构造器
//	 * @param obj
//	 */
//	@Deprecated
//	public Result(T obj) {
//		List<T> list = new ArrayList<T>();
//		list.add(obj);
//		this.setData(list);
//	}

	@Override
	public String toString(){
		return JsonUtil.toJson(this);
	}

	public T getData() {
		return data;
	}

	public Result setData(T data) {
		this.data = data;
		return this;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int errorCode) {
		this.code = errorCode;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String errorInfo) {
		this.info = errorInfo;
	}

    public String getCallback() {
        return callback;
    }

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public String getUnique() {
		return unique;
	}

	public void setUnique(String unique) {
		this.unique = unique;
	}
}
